001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Nov 27, 2002
005     * Time: 5:00:08 PM
006     */
007    
008    package EVolve.visualization.XYViz.ValRefViz.HotSpotViz;
009    
010    import EVolve.visualization.XYViz.ValRefViz.ValueReferenceVisualization;
011    import EVolve.visualization.*;
012    import EVolve.Scene;
013    import EVolve.util.painters.*;
014    import EVolve.util.phasedetectors.*;
015    import EVolve.data.Element;
016    import javax.swing.*;
017    import java.util.*;
018    import java.awt.*;
019    import java.awt.event.*;
020    
021    public class HotSpotVisualization extends ValueReferenceVisualization {
022        protected JTextField textInterval; // input for interval
023        protected JComboBox comboPainter;
024        protected Painter painters[];
025        protected int selectedPainter;
026        protected JPanel configurationPanel;
027        private JMenuItem itemSelectTimeFrame, itemSelectOccurredEntities, itemSelectAllEntities;
028        protected static JMenuItem selectionMenu[] = null;
029        protected static int SELECT_OPTION = 0x0011;
030    
031    
032        public HotSpotVisualization() {
033            interval = 1000;
034            painters = new Painter[2];
035            painters[0] = new DefaultPainter();
036            painters[1] = new RandomPainter();
037            selectedPainter = 0;
038            phaseDetector = new HotspotPhaseDetector();
039        }
040    
041        protected JPanel createConfigurationPanel() {
042            JPanel panelBottom = new JPanel(new GridLayout(2,2));
043            panelBottom.add(new JLabel("Interval: "));
044    
045            textInterval = new JTextField(String.valueOf(interval), 10);
046            panelBottom.add(textInterval);
047    
048            comboPainter = new JComboBox();
049            for (int i = 0; i < painters.length; i++) {
050                comboPainter.addItem(painters[i].getName());
051            }
052            comboPainter.addActionListener(new ActionListener() {
053                public void actionPerformed(ActionEvent e) {
054                    selectedPainter = comboPainter.getSelectedIndex();
055                }
056            });
057            panelBottom.add(new JLabel("Painter used:  "));
058            panelBottom.add(comboPainter);
059    
060    
061            if (configurationPanel == null)
062                configurationPanel = new JPanel(new FlowLayout());
063            configurationPanel.add(panelBottom);
064    
065            return configurationPanel;
066        }
067    
068        public void preVisualize() {
069            xMax = 0;
070            imageMap.clear();
071            currentThread = -1;
072            image = new AutoImage();
073            xOffset = -1;
074            installPainter();
075            timeMap.clear();
076            phaseDetector.reset();
077        }
078    
079        public void receiveElement(Element element) {
080            if (element.isOptional()) return;
081    
082            long x = xAxis.getField(element);
083            long y = yAxis.getField(element);
084            long z = 0;
085    
086            phaseDetector.collectData(x/interval,y);
087            if (selectedPainter != 0) // using random painter
088                paint(x,y,y);
089            else
090                paint(x,y,z);
091        }
092    
093        public void visualize() {
094            sort();
095        }
096    
097        public void makeSelection() {
098            if (SELECT_OPTION == 0) {
099                Scene.showErrorMessage("No data is to be selected.");
100                return;
101            }
102    
103            if (dataSourceId != Scene.getDataSourceManager().getCurrentDataSourceId()) {
104                Scene.showErrorMessage("The active data source used currently is different from \n" +
105                                       "this visualization, please choose \"" +
106                                       Scene.getDataSourceManager().getUsedDataSourceName(dataSourceId)+"\".");
107                return;
108            }
109    
110            int x1 = canvas.getStartX();
111            int x2 = canvas.getEndX();
112            int y1 = canvas.getEndY();
113            int y2 = canvas.getStartY();
114    
115            if (!normalOrientation) {
116                int temp;
117                temp = x1;
118                x1 = y1;
119                y1 = temp;
120                temp = x2;
121                x2 = y2;
122                y2 = temp;
123            }
124    
125            if (((x1<0)&&(x2<0)) || ((x1>=timeMap.size()))&&(x2>=timeMap.size()) ||
126                ((y1<0)&&(y2<0)) || ((y1>=yAxis.getEntityNumber())&&(y2>=yAxis.getEntityNumber())))
127               return;
128    
129            /*if (x1==x2) {
130                x2++;
131            }*/
132    
133            if (x1 < 0) {
134                x1 = 0;
135            }
136    
137            if (x1 > (timeMap.size() - 1)) {
138                x1 = timeMap.size() - 1;
139            }
140    
141            if (x2 > (timeMap.size() - 1)) {
142                x2 = timeMap.size() - 1;
143            }
144    
145            if (y1 < 0) {
146                y1 = 0;
147            }
148    
149            if (y1 > (yAxis.getEntityNumber() - 1)) {
150                y1 = yAxis.getEntityNumber() - 1;
151            }
152    
153            if (y2 > (yAxis.getEntityNumber() - 1)) {
154                y2 = yAxis.getEntityNumber() - 1;
155            }
156    
157            long start = ((long[])timeMap.get(x1))[1];
158            long end = ((long[])timeMap.get(x2))[1];
159    
160            if (((SELECT_OPTION & 0x000f) != SELECT_TIME_FRAME)||((x2==x1)&&(x1==0))) { // do not select time frame
161                start = 0;
162                end = Long.MAX_VALUE;
163            } else {
164                /*if (x2==x1) {
165                    x1--;
166                }
167                start = ((long[])timeMap.get(x1))[1];*/
168                if (x1==x2) {
169                    if (x2+1<timeMap.size()) {
170                        end = ((long[])timeMap.get(x2+1))[1];
171                    } else
172                        end = Long.MAX_VALUE;
173                }
174            }
175    
176            int[] selection = null;
177            switch (SELECT_OPTION & 0x0ff0) {
178                case 0x0100: // select all entities in the draw box
179                    selection = new int[y2 - y1 + 1];
180                    for (int i = y1; i <= y2; i++) {
181                        selection[i - y1] = i;
182                    }
183                    break;
184                case 0x0010: // select occurred entities
185                    ArrayList idList = new ArrayList();
186                    for (int i=y1; i<=y2; i++) {
187                        for (int j=x1; j<=x2; j++) {
188                            Color color;
189                            if (normalOrientation)
190                                color = image.getSortedColor(null,yAxis,j,i);
191                            else
192                                color = image.getSortedColor(yAxis,null,i,j);
193                            if (color != null) {
194                                idList.add(new Integer(i));
195                                break;
196                            }
197                        }
198                    }
199    
200                    selection = new int[idList.size()];
201                    for (int i=0; i<idList.size(); i++) {
202                        selection[i] = ((Integer)idList.get(i)).intValue();
203                    }
204                    break;
205                case 0x0000: // no selection on entities
206                    y1 = 0;
207                    y2 = yAxis.getEntityNumber() - 1;
208                    selection = new int[y2 - y1 + 1];
209                    for (int i = y1; i <= y2; i++) {
210                        selection[i - y1] = i;
211                    }
212                    break;
213            }
214    
215            yAxis.makeSelection(subjectDefinition.getType(),selection,start,end,timeMap);
216        }
217    
218        public void paint(long x, long y, long z) {
219            long temp = x;
220            if (x > xMax) {
221                xMax = x;
222            }
223    
224            if (xOffset == -1) xOffset = x/interval;
225    
226            x = x/interval;
227    
228            painter.paint(image,x-xOffset,y,z);
229    
230            countEvents(temp);
231        }
232    
233        protected void updateConfiguration() {
234            try {
235                if (autoInterval == -1)
236                    interval = Integer.parseInt(textInterval.getText());
237                else {
238                    interval = autoInterval;
239                    autoInterval = -1;
240                    textInterval.setText(String.valueOf(interval));
241                }
242                super.updateConfiguration();
243    
244            } catch (Exception e) {
245                Scene.showErrorMessage("Interval must be an integer");
246                configure();
247            }
248        }
249    
250        public JMenuItem[] createSelectionMenuItem() {
251            if (selectionMenu != null) return selectionMenu;
252    
253            itemSelectTimeFrame = new JCheckBoxMenuItem("Time Frame");
254            itemSelectTimeFrame.setMnemonic(KeyEvent.VK_T);
255            itemSelectTimeFrame.addActionListener(new ActionListener() {
256                public void actionPerformed(ActionEvent e) {
257                    boolean selected = itemSelectTimeFrame.isSelected();
258                    SELECT_OPTION = switchOption(selected, SELECT_OPTION, SELECT_TIME_FRAME);
259                }
260            });
261            itemSelectTimeFrame.setSelected(true);
262    
263            itemSelectOccurredEntities = new JCheckBoxMenuItem("Occurred Entities");
264            itemSelectOccurredEntities.setMnemonic(KeyEvent.VK_O);
265            itemSelectOccurredEntities.addActionListener(new ActionListener() {
266                public void actionPerformed(ActionEvent e) {
267                    boolean selected = itemSelectOccurredEntities.isSelected();
268                    SELECT_OPTION = switchOption(selected,SELECT_OPTION, SELECT_OCCURRED_ENTITIES);
269    
270                    /*if (itemSelectAllEntities.isSelected() && selected) {
271                        SELECT_OPTION = switchOption(false,SELECT_OPTION, SELECT_ALL_ENTITIES);
272                        itemSelectAllEntities.setSelected(false);
273                    }*/
274                }
275            });
276            itemSelectOccurredEntities.setSelected(true);
277    
278            itemSelectAllEntities = new JCheckBoxMenuItem("All Entities");
279            itemSelectAllEntities.setMnemonic(KeyEvent.VK_A);
280            itemSelectAllEntities.addActionListener(new ActionListener() {
281                public void actionPerformed(ActionEvent e) {
282                    boolean selected = itemSelectAllEntities.isSelected();
283                    SELECT_OPTION = switchOption(selected, SELECT_OPTION, SELECT_ALL_ENTITIES);
284    
285                    if (itemSelectOccurredEntities.isSelected() && selected) {
286                        SELECT_OPTION = switchOption(false,SELECT_OPTION, SELECT_OCCURRED_ENTITIES);
287                        itemSelectOccurredEntities.setSelected(false);
288                    }
289                }
290            });
291            itemSelectAllEntities.setSelected(true);
292    
293            selectionMenu = new JMenuItem[2];
294            selectionMenu[0] = itemSelectTimeFrame;
295            selectionMenu[1] = itemSelectOccurredEntities;
296            //selectionMenu[2] = itemSelectAllEntities;
297    
298            return selectionMenu;
299        }
300    
301        protected void installPainter() {
302            painter = painters[selectedPainter];
303        }
304    
305        public Object clone() {
306            HotSpotVisualization o = (HotSpotVisualization) super.clone();
307            o.configurationPanel = null;
308            o.panelConfiguration = o.createConfigurationPanel();
309            o.createDialog();
310            return o;
311        }
312    }